package com.embhealth.service.impl;


import com.alibaba.dubbo.config.annotation.Service;
import com.embhealth.dao.MenuDao;
import com.embhealth.dao.RoleDao;
import com.embhealth.entity.PageResult;
import com.embhealth.entity.QueryPageBean;
import com.embhealth.pojo.CheckGroup;
import com.embhealth.pojo.CheckItem;
import com.embhealth.pojo.Menu;
import com.embhealth.pojo.Permission;
import com.embhealth.service.MenuService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service(interfaceClass = MenuService.class)
@Transactional
public class MenuServiceImpl implements MenuService {
    @Autowired
    private RoleDao roleDao;
    @Autowired
    private MenuDao menuDao;

    @Override
    public List<Map<String, Object>> findByUsername(String username) {
        //根据用户名，查询角色id
        Integer Rid = roleDao.findRidByUsername(username);
        //根据角色id，获取可以访问的url
        List<Menu> parentUrl = menuDao.findParentUrl(Rid);
        //封装页面需要的数据
        List<Map<String, Object>> list = new ArrayList<>();
        for (Menu menu : parentUrl) {
            Map<String, Object> map = new HashMap<>();
            map.put("path", menu.getPath());
            map.put("title", menu.getName());
            map.put("icon", menu.getIcon());
            List<Menu> children = menu.getChildren();
            List<Map<String, Object>> childlist = new ArrayList<>();
            for (Menu child : children) {
                Map<String, Object> map2 = new HashMap<>();
                map2.put("path", child.getPath());
                map2.put("title", child.getName());
                map2.put("linkUrl", child.getLinkUrl());
                childlist.add(map2);
            }
            map.put("children", childlist);
            list.add(map);
        }
        return list;
    }

    /**
     * 分页查询
     *
     * @param queryPageBean
     * @return
     */
    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        String queryString = queryPageBean.getQueryString();
        //完成分页查询基于mybatis框架的PageHelper分页助手
        PageHelper.startPage(currentPage, pageSize);
        Page<Menu> page = menuDao.findPage(queryString);
        return new PageResult(page.getTotal(), page.getResult());
    }

    @Override
    public PageResult findAllMenByRole(Integer id, QueryPageBean queryPageBean) {
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        String queryString = queryPageBean.getQueryString();
        //完成分页查询基于mybatis框架的PageHelper分页助手
        PageHelper.startPage(currentPage, pageSize);
        Page<Menu> page = menuDao.findAllMenByRole(id);
        return new PageResult(page.getTotal(), page.getResult());
    }

    /**
     * 根据menu id回显菜单数据
     *
     * @param id
     * @return
     */
    @Override
    public Menu handleUpdate(Integer id) {
        return menuDao.handleUpdate(id);
    }

    /**
     * 添加菜单
     *
     * @param menu
     */
    @Override
    public void handleAdd(Menu menu) {
        menuDao.handleAdd(menu);
    }

    /**
     * 编辑更新菜单
     *
     * @param menu
     */
    @Override
    public void handleEdit(Menu menu) {
        menuDao.handleEdit(menu);
    }

    /**
     * 根据id删除菜单项
     *
     * @param id
     */
    @Override
    public boolean handleDelete(Integer id) {
        //通过菜单id查询是否有与其他表的关联关系
        Integer count = menuDao.findRoleByMenuId(id);
        if (count > 0) {
            return false;
        }
        menuDao.handleDelete(id);
        return true;
    }

    @Override
    public PageResult queryPage(QueryPageBean queryPageBean){
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        String queryString = queryPageBean.getQueryString();
        //完成分页查询基于mybatis框架的PageHelper分页助手
        PageHelper.startPage(currentPage,pageSize);
        Page<Menu> page = menuDao.queryPage();
        return new PageResult(page.getTotal(),page.getResult());
    }
}
